package Main;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import DTO.Table;
import XML.XMLHelper;

public class Database {
	
	private String name;
	private Map<String,Table> tables;	

	public Database(String name)
	{
		this.tables = new Hashtable<String, Table>();
		this.name = name; 
	}
	
	/**
	 * Save all table to database (chua hoan thanh)
	 */
	public void save()
	{
		ArrayList<Table> allTable = getTableArrayList();
		
		// Save tables to data
		for(int i = 0 ; i < allTable.size(); i++)
		{
			allTable.get(i).save();
		}
	}
	
	/**
	 * Load all table from database
	 */
	public void load()
	{		
		//Load all tables struct , and data (if exists)
		File tableFiles = new File("Database\\" + this.name);		
		File[] childs = tableFiles.listFiles();
		
		for(int i = 0 ; i < childs.length; i++)
		{
			File structChild = childs[i];			
			String tableName = structChild.getName();
			if(!(tableName).contains("data.xml"))
			{	
				String name = tableName.replace(".xml", "");
				Table table = new Table(name, this.name, null, null);
				table.load();
				this.addTable(table);
			}
		}
	}
	
	/**
	 * Get table by its name
	 * @param name Name of table
	 * @return return 
	 * Return table instance if success!
	 * Return null if failed!
	 */
	public Table getTableByName(String name)
	{
		if(tables.containsKey(name))
			return tables.get(name);
		
		return null;
	}
	
	/**
	 * Get array list of tables
	 * @return
	 * Return tables if success!
	 * Return null if failed!
	 */
	public ArrayList<Table> getTableArrayList()
	{
		ArrayList<Table> tableAL = new ArrayList<Table>();		
		tableAL.addAll(tables.values());
		return tableAL;
	}	
	
	/**
	 * Add new table to database
	 * @param table Table need add to this database
	 * @return
	 * Return table which added to database
	 * Return null if add failed!
	 */
	public Table addTable(Table table)
	{
		return tables.put(table.get_Name(), table);
		
	}
	
	/**
	 * Execute query (chua hoan thanh)
	 * @return
	 */
	public boolean executeQuery()
	{		
		return false;
	}
	
	/**
	 * Get name of this database
	 * @return Return name of this database
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Set name for this database
	 * @param name New name for database
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	
	/*
	 * Static Area
	 */
	/**
	 * Static: Create new database. It only create new folder which name is name of 
	 * database. Root folder of database is "Database".
	 * @param name Name of database
	 * @return
	 * Return database which is created
	 * Return null if database exists !
	 */
	public static Database createDatabase(String name)
	{
		Database db = new Database(name);
		if(XMLHelper.CreateDatabase(name)){
			return db;
		}

		if(XMLHelper.IsExist(name))
			return db;
		
		return null;
	}
}
